package cn.geekyspace.scanhive.handler;


import cn.geekyspace.scanhive.utils.R;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public R<?> ex(Exception e) {
        return R.error(e.getMessage());
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R<?> ex(MethodArgumentNotValidException e) {
        return R.error(e.getBindingResult().getFieldError().getDefaultMessage());
    }

}